home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / utilities / print / pspager.lha / psp / pspager.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-21  |  8.2 KB  |  342 lines

  1. /* pspager.c */
  2.  
  3. #include "ps_package.h"
  4. #include "pspager.h"
  5. #ifdef AMIGA
  6. #include <utilities.h>
  7. #include <getopt.h>
  8. #else
  9. #include "utilities.h"
  10. #include "getopt.h"
  11. #endif
  12. #include <string.h>
  13. #include <sys/stat.h>
  14.  
  15.  
  16. /* preprocessor symbols */
  17.  
  18. #define UNTITLED   "Untitled"
  19. #define PSP_ADOBE  "psp_adobe.ps"
  20. #define PSP_SETUP  "psp_setup.ps"
  21.  
  22. #define PAGE_LIMIT_BBE   56
  23. #define PAGE_LIMIT_SAT2  74
  24. #define PAGE_LIMIT_SAT4  70
  25.  
  26.  
  27. /* global data */
  28.  
  29. static char *fileName = NULL;
  30. static time_t fileTime;
  31. static int pageNo;
  32. static char satFonts[] = "Helvetica Helvetica-Bold Courier";
  33. static char bbeFonts[] = "Courier-Bold Courier";
  34.  
  35.  
  36. /* postscript generation functions */
  37.  
  38. static void Print1PerPage(void)
  39. {
  40.   char *ln,dateStr[40],timeStr[40];
  41.   int i,j,y;
  42.  
  43.   strftime(dateStr,40,"%A, %d. %B %Y",localtime(&fileTime));
  44.   strftime(timeStr,40,"%X Uhr",localtime(&fileTime));
  45.  
  46.   ln = PSNextLine();
  47.   for(i=0; i<pageNo; i++) {
  48.     /* print page skeleton */
  49.     printf("%%%%Page: %d %d\n"
  50.       "%%%%BeginPageSetup\n"
  51.       "initializepage\n"
  52.       "(pspager; page: %d of %d)setjob\n"
  53.       "%%%%EndPageSetup\n"
  54.       "gS 0 0 552 730 rC\n"
  55.       "5 16 :M\n"
  56.       "f1_9 sf\n"
  57.       ".609(%s)A\n"
  58.       "470 16 :M\n"
  59.       "1.434 .143(Page %d of %d)J\n"
  60.       "5 27 :M\n"
  61.       "2.466 .247(%s)J\n"
  62.       "463 27 :M\n"
  63.       "2.981 .298(%s)J\n"
  64.       "5 38.25 -.25 .25 547.25 38 .25 5 38 @a\n"
  65.       "5 40.25 -.25 .25 547.25 40 .25 5 40 @a\n"
  66.       "1 G\n"
  67.       "0 0 0 0 rC\n"
  68.       "0 0 0 0 rF\n"
  69.       "-1 0 1 12 rF\n"
  70.       "gR\n"
  71.       "1 G\n"
  72.       "gS 16 49 531 672 rC\n"
  73.       "16 49 531 672 rF\n"
  74.       "15 49 532 12 rF\n"
  75.       "16 58 :M\n"
  76.       "0 G\n"
  77.       "f3_9 sf\n",
  78.       i+1,i+1,i+1,pageNo,fileName,i+1,pageNo,dateStr,timeStr);
  79.  
  80.     /* print pane contents */
  81.     for(j=0,y=58; j<PAGE_LIMIT_BBE && ln && ln[0]!='\f'; j++) {
  82.       if(j > 0) {
  83.         printf("16 %d 531 12 rF\n", y-9);
  84.         if(ln[0] == '\0') {
  85.           y += 12;
  86.           ln = PSNextLine();
  87.           continue; /* don't print anything for empty lines */
  88.         }
  89.         printf("16 %d :M\n0 G\n", y);
  90.       }
  91.       printf("(%s)S\n1 G\n",ln);
  92.       y += 12;
  93.       ln = PSNextLine();
  94.     }
  95.  
  96.     /* skip rest of pane at form feed */
  97.     if(ln && ln[0]=='\f')
  98.       ln++;
  99.     printf("endp\n");
  100.   }
  101. }
  102.  
  103.  
  104. static void Print2PerPage(void)
  105. {
  106.   char *ln,*userStr,dateStr[80];
  107.   int i,j,x,xc,y,m,d;
  108.   static const char * const pnSetup[2] = {
  109.                 "0  18 385 518 rC\n"   "1  23 :M\n" "f2_7 sf\n",
  110.     "gR\n" "gS 395  18 386 518 rC\n" "396  23 :M\n" "f2_7 sf\n"
  111.   };
  112.   static const struct { int x,y; } pnCoords[2] = {
  113.     { 1, 23 }, { 396, 23 }
  114.   };
  115.  
  116.   if(!(userStr = getenv("USER")))
  117.     userStr = "Nobody";
  118.   strftime(dateStr,80,"%d.%m %y@%H:%M by ",localtime(&fileTime));
  119.   strcat(dateStr,userStr);
  120.  
  121.   /* estimate a x position for title */
  122.   m = strlen(fileName);
  123.   xc = m>7 ? 368-(m-7)*6 : 368+(7-m)*6;
  124.  
  125.   ln = PSNextLine();
  126.   for(i=0; i<pageNo; i++) {
  127.     m = i & 1;
  128.     /* print page skeleton every four panes*/
  129.     if(m == 0) {
  130.       d = i >> 1;
  131.       printf("%%%%Page: %d %d\n"
  132.         "%%%%BeginPageSetup\n"
  133.         "initializepage\n"
  134.         "(pspager; page: %d of %d)setjob\n"
  135.         "%%%%EndPageSetup\n"
  136.         "gS 0 0 781 538 rC\n"
  137.         "0 0 215 13 rC\n"
  138.         "1 10 :M\n"
  139.         "f0_12 sf\n"
  140.         ".815 .082(%s)J\n"
  141.         "gR\n"
  142.         "gS 0 0 781 538 rC\n"
  143.         /*"%d 10 :M\n"*/
  144.         "f1_12 sf\n"
  145.         "390 (%s) stringwidth pop 2 div sub 10 :M\n"
  146.         ".0(%s)A\n"
  147.         "774 10 :M\n"
  148.         "f0_12 sf\n"
  149.         "(%d)S\n"
  150.         "0 13.25 -.25 .25 781.25 13 .25 0 13 @a\n"
  151.         "-.25 -.25 390.25 538.25 .25 .25 390 13 @b\n",
  152.         d+1,d+1,d+1,pageNo,dateStr,fileName,fileName,d+1); /*xc*/
  153.     }
  154.  
  155.     /* setup current pane */
  156.     x = pnCoords[m].x;
  157.     y = pnCoords[m].y;
  158.     printf("%s",pnSetup[m]);
  159.  
  160.     /* print pane contents */
  161.     for(j=0; j<PAGE_LIMIT_SAT2 && ln && ln[0]!='\f'; j++) {
  162.       printf("%d %d :M\n(%s)S\n",x,y,ln);
  163.       y += 7;
  164.       ln = PSNextLine();
  165.     }
  166.  
  167.     /* skip rest of pane at form feed */
  168.     if(ln && ln[0]=='\f')
  169.       ln++;
  170.  
  171.     /* print end of page after each second pane */
  172.     if(m==1 || !ln)
  173.       printf("endp\n");
  174.   }
  175. }
  176.  
  177.  
  178. static void Print4PerPage(void)
  179. {
  180.   char *ln,*userStr,dateStr[80];
  181.   int i,j,x,xc,y,m,d;
  182.   static const char * const pnSetup[4] = {
  183.                 "0  18 271 355 rC\n"   "1  22 :M\n" "f2_5 sf\n",
  184.     "gR\n" "gS 281  18 271 355 rC\n" "282  22 :M\n" "f2_5 sf\n",
  185.     "gR\n" "gS   0 376 271 355 rC\n"   "1 380 :M\n" "f2_5 sf\n",
  186.     "gR\n" "gS 281 376 271 355 rC\n" "282 380 :M\n" "f2_5 sf\n"
  187.   };
  188.   static const struct { int x,y; } pnCoords[4] = {
  189.     { 1, 22 }, { 282, 22 }, { 1, 380 }, { 282, 380 }
  190.   };
  191.  
  192.   if(!(userStr = getenv("USER")))
  193.     userStr = "Nobody";
  194.   strftime(dateStr,80,"%d.%m %y@%H:%M by ",localtime(&fileTime));
  195.   strcat(dateStr,userStr);
  196.  
  197.   /* estimate a x position for title */
  198.   m = strlen(fileName);
  199.   xc = m>10 ? 233-(m-10)*6 : 233+(10-m)*6;
  200.  
  201.   ln = PSNextLine();
  202.   for(i=0; i<pageNo; i++) {
  203.     m = i % 4;
  204.     /* print page skeleton every four panes*/
  205.     if(m == 0) {
  206.       d = i/4;
  207.       printf("%%%%Page: %d %d\n"
  208.         "%%%%BeginPageSetup\n"
  209.         "initializepage\n"
  210.         "(pspager; page: %d of %d)setjob\n"
  211.         "%%%%EndPageSetup\n"
  212.         "gS 0 0 552 730 rC\n"
  213.         "0 0 215 13 rC\n"
  214.         "1 10 :M\n"
  215.         "f0_12 sf\n"
  216.         ".907 .091(%s)J\n"
  217.         "gR\n"
  218.         "gS 0 0 552 730 rC\n"
  219.         "f1_12 sf\n"
  220.         /*"%d 10 :M\n"*/
  221.         "276 (%s) stringwidth pop 2 div sub 10 :M\n"
  222.         ".0(%s)A\n"
  223.         "537 10 :M\n" /*545 10 for normal printers*/
  224.         "f0_12 sf\n"
  225.         "(%d)S\n"
  226.         "0 13.25 -.25 .25 552.25 13 .25 0 13 @a\n"
  227.         "-.25 -.25 276.25 730.25 .25 .25 276 13 @b\n"
  228.         "0 371.25 -.25 .25 552.25 371 .25 0 371 @a\n",
  229.         d+1,d+1,d+1,pageNo,dateStr,fileName,fileName,d+1); /*xc*/
  230.     }
  231.  
  232.     /* setup current pane */
  233.     x = pnCoords[m].x;
  234.     y = pnCoords[m].y;
  235.     printf("%s",pnSetup[m]);
  236.  
  237.     /* print pane contents */
  238.     for(j=0; j<PAGE_LIMIT_SAT4 && ln && ln[0]!='\f'; j++) {
  239.       printf("%d %d :M\n(%s)S\n",x,y,ln);
  240.       y += 5;
  241.       ln = PSNextLine();
  242.     }
  243.  
  244.     /* skip rest of pane at form feed */
  245.     if(ln && ln[0]=='\f')
  246.       ln++;
  247.  
  248.     /* print end of page every four panes */
  249.     if(m==3 || !ln)
  250.       printf("endp\n");
  251.   }
  252. }
  253.  
  254.  
  255. int main(int argc, char *argv[])
  256. {
  257.   static void (*actionFunc)(void);
  258.   char *fontNames;
  259.   Orientation orient;
  260.   struct stat fs;
  261. #ifdef AMIGA
  262.   struct tm *ft;
  263. #endif
  264.   enum { eCurDate, eModifDate } prtDate;
  265.   FILE *fp;
  266.   int ch;
  267.  
  268.   /* assume 2 per page, use modification date */
  269.   actionFunc = NULL;
  270.   fontNames = satFonts;
  271.   orient = eLandscape;
  272.   prtDate = eModifDate;
  273.  
  274.   /* parse options */
  275.   while((ch = getopt(argc,argv,"f:d124")) != EOF)
  276.     switch(ch) {
  277.       case '1':
  278.         PSDefSymbol("PSP_1_PER_PAGE");
  279.         actionFunc = Print1PerPage;
  280.         fontNames = bbeFonts;
  281.         orient = ePortrait;
  282.         break;
  283.       case '2':
  284.         PSDefSymbol("PSP_2_PER_PAGE");
  285.         actionFunc = Print2PerPage;
  286.         fontNames = satFonts;
  287.         orient = eLandscape;
  288.         break;
  289.       case '4':
  290.         PSDefSymbol("PSP_4_PER_PAGE");
  291.         actionFunc = Print4PerPage;
  292.         fontNames = satFonts;
  293.         orient = ePortrait;
  294.         break;
  295.       case 'f':
  296.         fileName = optarg; /* input file specification */
  297.         break;
  298.       case 'd':
  299.         prtDate = eCurDate; /* current date instead of modification date */
  300.         break;
  301.       default:
  302.         ; /*nothing to do*/
  303.     }
  304.  
  305.   if(!actionFunc) {
  306.     PSDefSymbol("PSP_2_PER_PAGE");
  307.     actionFunc = Print2PerPage;
  308.   }
  309.  
  310.   /* setup global data */
  311.   if(fileName) {
  312.     if(stat(fileName,&fs) < 0)
  313.       FatalExit("cannot locate input file");
  314. #ifdef AMIGA
  315.     ft = localtime(&fs.st_mtime);
  316.     ft->tm_hour += 6;  /* bug in SAS/C function fstat() - 6 hours too late */
  317.     fs.st_mtime = mktime(ft);
  318. #endif
  319.     if(!(fp = fopen(fileName,"r")))
  320.       FatalExit("cannot open input file");
  321.     fileTime = prtDate==eModifDate ? fs.st_mtime : time(NULL);
  322.   }
  323.   else {
  324.     fp = stdin;
  325.     fileName = UNTITLED;
  326.     fileTime = time(NULL);
  327.   }
  328.  
  329.   /* read input data and print postscript source */
  330.   pageNo = PSReadFile(fp);
  331.   PSPrintHdr(fileName,fileTime,pageNo,fontNames,orient);
  332.   PSInclFile(TackOn(PSP_LIBDIR,PSP_SETUP));
  333.   PSUndefAll();
  334.   (*actionFunc)();
  335.   PSPrintEnd();
  336.   PSFreeFile();
  337.   if(fp != stdin)
  338.     fclose(fp);
  339.  
  340.   return 0;
  341. }
  342.